package sk.tuke.magsa.framework.ui;

import java.util.List;
import sk.tuke.magsa.framework.ApplicationException;
import sk.tuke.magsa.framework.CRUDDao;
import sk.tuke.magsa.framework.Entity;
import sk.tuke.magsa.framework.Utilities;

public abstract class TableDialog<T extends Entity> {
    private final CRUDDao<T> dao;

    public TableDialog(CRUDDao<T> dao) {
        this.dao = dao;
    }

    public void menu() {
        int selection = 0;
        do {
            printMenu();
            try {
                selection = Integer.parseInt(Utilities.readLine());
            } catch (NumberFormatException e) {
                //Do nothing, repeat loop
            }

            switch (selection) {
                case 1:
                    display();
                    break;
                case 2:
                    displaySelectedItems();
                    break;
                case 3:
                    create();
                    break;
                case 4:
                    edit();
                    break;
                case 5:
                    try
                    {
                        remove();
                    }
                    catch (ApplicationException e)
                    {
                        if(e.getCode() == 13)
                        {
                            System.out.println("There is some trouble with removing item. Probably some other items reference to it.");
                        }
                    }
                    break;
            }
        } while (selection != 6);
    }

    public T selection() {
        printTable(dao.selectAll());
        return selectEntity();
    }

    protected void printMenu() {
        System.out.println("(1) Display");
        System.out.println("(2) Search by own input");
        System.out.println("(3) Create");
        System.out.println("(4) Edit");
        System.out.println("(5) Remove");
        System.out.println("(6) Return");
        System.out.println("Enter selection: ");
    }
    
    

    public void display() {
        printTable(dao.selectAll());
    }
    
    public void displaySelectedItems()
    {
        printNumberPropertiesMenu();
        Integer id = null;
        String str = null;
        do {
//            System.out.println("Enter identifier [empty string = no selection]: ");
            String input = Utilities.readLine();
            if ("".equals(input)) {
//                return null;
                continue;
            }
            
            try {
                id = Integer.valueOf(input);
            } catch (NumberFormatException e) {
                continue;
            }          
        } while (id == null);
            System.out.println("Enter value of selected variable");
        do {
//            System.out.println("Enter identifier [empty string = no selection]: ");
            String input = Utilities.readLine();
            if ("".equals(input)) {
//                return null;
                continue;
            }
                str = input;
                      
        } while (str == null);
        
        printTable(dao.findByProperty(str, id));
    }

    public void create() {
        T entity = createFormDialogForInsert().show();
        dao.create(entity);
    }

    public void edit() {
        T entity = selectEntity();
        if (entity != null) {
            createFormDialogForEdit(entity).show();
            dao.edit(entity);
        }
    }

    public void remove() {
        T entity = selectEntity();
        if (entity != null) {
            dao.remove(entity);
        }
    }

    protected T selectEntity() {
        T entity = null;
        do {
            System.out.println("Enter identifier [empty string = no selection]: ");
            String input = Utilities.readLine();
            if ("".equals(input)) {
                return null;
            }
            Integer id = null;
            try {
                id = Integer.valueOf(input);
            } catch (NumberFormatException e) {
                continue;
            }
            entity = dao.find(id);            
        } while (entity == null);
        return entity;
    }
    //toto dokoncit, aby som vedel vyhladavat podla toho co ja chcem :)
//    protected List<T> selectEntityByOwnInput() {
//        List<T> entity = null;
//        do {
//            System.out.println("Enter identifier [empty string = no selection]: ");
//            String input = Utilities.readLine();
//            if ("".equals(input)) {
//                return null;
//            }
//            Object id = null;
//            try {
//                id = Integer.valueOf(input);
//            } catch (NumberFormatException e) {
//                try 
//                {
//                    id = Double.valueOf(input);
//                } 
//                catch (NumberFormatException ee) 
//                {
//                    id = input;
//                }
//            }
//            
////            Double idDob = null;
////            try {
////                idDob = Double.valueOf(input);
////            } catch (NumberFormatException e) {
////                continue;
////            }
//            
////            String idstr = null;
////                idstr = input;
//            
//            entity = dao.findString(id);            
//            
//        } while (entity == null);
//        return entity;
//    }

    public void printTable(List<T> list) {
        printHeader();
        for (T entity : list) {
            printRow(entity);
        }
    }

    protected abstract FormDialog<T> createFormDialogForInsert();

    protected abstract FormDialog<T> createFormDialogForEdit(T entity);

    protected abstract void printHeader();

    protected abstract void printRow(T entity);
    
    protected abstract void printNumberPropertiesMenu();
}
